package com.processmining.support.analyseSupport.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.deckfour.xes.model.XLog;

import com.processmining.support.analyseSupport.activity.impl.ActivityImpl;

import net.ProcessMining.mining.heuristics.HeuristicsMetrics;
import net.ProcessMining.models.heuristics.SimpleHeuristicsNet;

/**
 * 力导向图表示资源间的联系
 * @author admin
 *
 */
public class ForcedirectedDiagram {
	private SimpleHeuristicsNet net;
	private String resourceAttr;
	private Map<String,ForcedirectedResource> forcedirectedResources;
	private XLog log;
	public ForcedirectedDiagram(SimpleHeuristicsNet net, String resourceAttr,XLog log) {
		super();
		this.net = net;
		this.resourceAttr = resourceAttr;
		forcedirectedResources=new HashMap<String,ForcedirectedResource>();
		this.log=log;
	}
	public void createForcedirectedDiagram(int attrType){
		ActivityImpl activityImpl=new ActivityImpl();
		HeuristicsMetrics metrics = this.net.getMetrics();
		int size = net.size();
		Map<Integer, String> names = new HashMap<Integer, String>();
		for (int i = 0; i < size; i++) {
			names.put(i, net.getActivitiesMappingStructures()
					.getXEventClasses().getByIndex(i).toString());
		}
		Map<String,String> resourceNames=activityImpl.getEventAttribute(log, names, resourceAttr, attrType);
		for (int i = 0; i < size; i++) {
			String resourceName=resourceNames.get(names.get(i));
			ForcedirectedResource resource = new ForcedirectedResource(resourceName);
			Map<String,ForcedirectedItem> relatedResources=new HashMap<String,ForcedirectedItem>();
			// input
			int inputSetSize = net.getInputSet(i).size();
			for (int j = 0; j < inputSetSize; j++) {
				int subSetSize = net.getInputSet(i).get(j).size();
				for (int k = 0; k < subSetSize; k++) {
					int element = net.getInputSet(i).get(j).get(k);
					String resourceName1=resourceNames.get(names.get(element));
					if(resourceName1.equals(resourceName)){
						continue;
					}
					//System.out.println("counts:"+metrics.getDirectSuccessionCount(i, element));
					if(relatedResources.containsKey(resourceName1)){
						long counts=relatedResources.get(resourceName1).getCounts()+(long) metrics.getDirectSuccessionCount(i, element);
						relatedResources.get(resourceName1).setCounts(counts);
					}else{
						ForcedirectedItem item=new ForcedirectedItem(resourceName1,(long) metrics.getDirectSuccessionCount(i, element));
						relatedResources.put(resourceName1, item);
					}
					
				}
			}
			// 组织活动的output
			int outputSetSize = net.getOutputSet(i).size();
			for (int j = 0; j < outputSetSize; j++) {
				int subSetSize = net.getOutputSet(i).get(j).size();
				for (int k = 0; k < subSetSize; k++) {
					int element = net.getOutputSet(i).get(j).get(k);
					String resourceName1=resourceNames.get(names.get(element));
					if(resourceName1.equals(resourceName)){
						continue;
					}
					if(relatedResources.containsKey(resourceName1)){
						long counts=relatedResources.get(resourceName1).getCounts()+(long) metrics.getDirectSuccessionCount(i, element);
						relatedResources.get(resourceName1).setCounts(counts);
					}else{
						ForcedirectedItem item=new ForcedirectedItem(resourceName1,(long) metrics.getDirectSuccessionCount(i, element));
						relatedResources.put(resourceName1, item);
					}
				}
			}
			resource.setRelatedResources(relatedResources);
			forcedirectedResources.put(resourceName, resource);
		}
	}
	@Override
	public String toString() {
		StringBuffer sb=new StringBuffer();
		for (Map.Entry<String, ForcedirectedResource> entry : forcedirectedResources.entrySet()) {
             //Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
             //entry.getKey() ;entry.getValue(); entry.setValue();
             //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
             System.out.println("key= " + entry.getKey() + " and value= ");
             ForcedirectedResource resource=entry.getValue();
             for (Map.Entry<String, ForcedirectedItem> entry1 : resource.getRelatedResources().entrySet()){
            	 System.out.println(entry1.getValue().toString());
             }
         }
		return "ForcedirectedDiagram [resourceAttr=" + resourceAttr
				+ ", forcedirectedResources=" + forcedirectedResources + "]";
	}
	
}
